package com.simba.server.framedecoder.decoders;

import com.simba.server.components.CommandExecutor;
import com.simba.server.components.MachineParams;
import org.apache.log4j.Logger;
import org.jboss.netty.channel.Channel;


import com.simba.common.decoder.Decoder;
import com.simba.common.log.LogUtil;

import com.simba.server.commands.impl.record.RecordMachineOpsCmd;
import com.simba.server.commands.impl.update.RenewBasicConfigCmd;
import com.simba.server.components.SortControllerManager;
import com.simba.server.components.data.BasicConfig;
import com.simba.server.components.data.MachineOps;

public class MachineStatusDecoder implements Decoder
{
	private static class MachineStatusDecoderHolder
	{
		private static MachineStatusDecoder INSTANCE = new MachineStatusDecoder();
	}

	public static final String NAME = "MachineStatus";
	protected final static Logger logger = Logger.getLogger(MachineStatusDecoder.class);
	private static final int MACHINE_STATUS_POSITION = 3;
	private static final int MACHINE_PAUSE = 3;
	private static final int MACHINE_RUNNING = 5;
	private static final int MACHINE_STARTED = 1;
	private static final int MACHINE_STARTING = 6;
	private static final int MACHINE_STOPPED = 2;
	private static final int MACHINE_WAIT = 4;

	public static MachineStatusDecoder getInstance()
	{
		return MachineStatusDecoderHolder.INSTANCE;
	}

	private MachineStatusDecoder()
	{

	}

	@Override
	public void doDecode(Channel channel, Object message)
	{
		try
		{
			byte[] content = (byte[]) message;
			int machineStatus = content[MachineStatusDecoder.MACHINE_STATUS_POSITION];
			SortControllerManager.getInstance().onMachineStateMessage(machineStatus);
			switch (machineStatus)
			{
				case MACHINE_STARTED:
					MachineParams.getInstance().setMachineStatus("started");
					this.renewMachineStatus("started");
					this.recordMachineAction(MachineStatusDecoder.MACHINE_STARTED, "started");
					break;
				case MACHINE_STOPPED:
					MachineParams.getInstance().setMachineStatus("stopped");
					this.renewMachineStatus("stopped");
					this.recordMachineAction(MachineStatusDecoder.MACHINE_STOPPED, "stopped");
					break;
				case MACHINE_PAUSE:
					MachineParams.getInstance().setMachineStatus("pause");
					this.renewMachineStatus("pause");
					break;
				case MACHINE_WAIT:
					MachineParams.getInstance().setMachineStatus("wait");
					this.renewMachineStatus("wait");
					break;
				case MACHINE_RUNNING:
					this.renewMachineStatus("running");
					MachineParams.getInstance().setMachineStatus("running");
					break;
				case MACHINE_STARTING:
					MachineParams.getInstance().setMachineStatus("starting");
					break;
				default:
					MachineParams.getInstance().setMachineStatus("none");
					break;
			}
		}
		catch (Exception ex)
		{
			LogUtil.LogError(MachineStatusDecoder.logger,
					"MachineStatus Decode Catch Exception : " + ex.getMessage());
		}
	}

	@Override
	public String getName()
	{
		return null;
	}

	private void recordMachineAction(int opsType, String opsDesc)
	{
		try
		{
			MachineOps machineOps = new MachineOps();
			machineOps.setWorkerNo("none");
			machineOps.setOpsType(opsType);
			machineOps.setOpsDesc(opsDesc);

			CommandExecutor.executorSqlCommand(RecordMachineOpsCmd.NAME, machineOps,false);
		}
		catch (Exception ex)
		{
			LogUtil.LogError(MachineStatusDecoder.logger,
					"Record machine action catch exception : " + ex.getMessage());
		}
	}

	private void renewMachineStatus(String value)
	{
		String machineStatus = MachineParams.getInstance().getMachineStatus();
		if (machineStatus.equals("running") && value.equals("running"))
		{
			return;
		}
		BasicConfig basicConfig = new BasicConfig();
		basicConfig.setBasicKey("runStatus");
		basicConfig.setBasicValue(value);
		CommandExecutor.executorSqlCommand(RenewBasicConfigCmd.NAME, basicConfig,false);

	}

}
